﻿<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeFile="ChiTietPhieuBanHang.aspx.cs"
    Inherits="WebTeamplate.Admin_ChiTietPhieuBanHang" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <script language="javascript" type="text/javascript" src="../Js/actb.js"></script>

        <script language="javascript" type="text/javascript" src="../Js/commonAutocomplete.js"></script>

        <script src="../Js/BasicFunction.js"></script>

        <script src="../Js/ClientScript.js"></script>

        <script src="../Js/formatNumber.js"></script>

        <link href="../Js/calendar/calendar-win2k-cold-1.css" rel="stylesheet" type="text/css" />

        <script src="../Js/DateFunction.js"></script>

        <script type="text/javascript" src="../Js/calendar/calendar.js"></script>

        <script type="text/javascript" src="../Js/calendar/calendar-setup.js" language="javascript"></script>

        <script type="text/javascript" src="../Js/calendar/lang/calendar-en.js"></script>

        <script>
            function RowHover(obj) {
                if (obj)
                    obj.className = 'lvtColDataHover';
            }
            function RowOut(obj) {
                if (obj)
                    obj.className = 'lvtColData';
            }
            function TextBoxFocus(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxListOn1 txtNumber';
            }
            function TextBoxBlurGroupNumber(obj) {

                if (obj) {
                    obj.className = 'detailedViewTextBoxList1 txtNumber';
                    obj.value = formatNumberAutoSeparate(obj.value);
                }
            }
            function TextBoxBlur(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxList1 txtNumber';
            }
            function TextBoxLeftFocus(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxListOn1';
            }
            function TextBoxLeftBlur(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxList1';
            }
        </script>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <table class="listForm" border="0" cellpadding="0" cellspacing="1" width="1200px"
            align="center">
            <tr>
                <td colspan="6" class="detailedViewHeader">
                    <b>Quản lý phiếu bán hàng</b>
                </td>
            </tr>
            <tr class="rowDetail" align="right">
                <td colspan="6" class="dvtCellInfo">
                    (<span class="commentRequireInput">*</span>): <span class="commentRequireInputText">
                        <%=Resources.Resource.LABEL_COMMENT_REQUIRE %>
                    </span>
                </td>
            </tr>
            <tr class="rowDetail">
                <td class="dvtCellLabel" width="150px" align="right">
                    <asp:Label ID="lblKho" runat="server" Text="Khu"></asp:Label>(<span class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="150px" align="left">
                    <asp:DropDownList ID="drlKho" Width="140px" onchange="ChangeKho(this.value);" onkeyup="this.blur();this.focus();"
                        CssClass="listForm ctrListForm" runat="server">
                    </asp:DropDownList>
                </td>
                <td class="dvtCellLabel" width="150px" align="right">
                    Ngày lập phiếu(<span class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="150px" align="left">
                    <asp:TextBox ID="txtNgayLapPhieu" runat="server" Width="90px" MaxLength="10" onblur="this.className='detailedViewTextBoxList1'"
                        onfocus="this.className='detailedViewTextBoxListOn1'" CssClass="detailedViewTextBoxList1"></asp:TextBox>
                    <img id="imgNgayLapPhieu" alt="" title="Ngày nhập" style="cursor: hand; text-align: left;
                        vertical-align: middle; margin-bottom: 4px;" src="../images/imgCalendar.gif" />
                </td>
                <td class="dvtCellLabel" width="160px" align="right">
                    <asp:Label ID="lblNguoiNhap" runat="server" Text="Người viết phiếu"></asp:Label>(<span
                        class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="160px" align="left">
                    <asp:TextBox ID="txtNguoiNhap" runat="server" Width="180px" MaxLength="100" Enabled="false"
                        Text="Nguyen Van Ty" CssClass="detailedViewTextBoxList_Disable1 ctrListForm"></asp:TextBox>
                </td>
            </tr>
            <tr class="rowDetail">
                <td class="dvtCellLabel">
                    <asp:Label ID="lblTenKhachHang" runat="server" Text="Tên khách hàng"></asp:Label>:
                </td>
                <td class="dvtCellInfo">
                    <asp:TextBox ID="txtTenKhachHang" runat="server" Width="180px" MaxLength="100" Text=""
                        CssClass='detailedViewTextBoxList ctrListForm' onfocus='TextBoxLeftFocus(this);'
                        onblur='TextBoxLeftBlur(this);'></asp:TextBox>
                </td>
                <td class="dvtCellLabel">
                    <asp:Label ID="lblDiaChi" runat="server" Text="Địa chỉ"></asp:Label>:
                </td>
                <td class="dvtCellInfo" colspan="3">
                    <asp:TextBox ID="txtDiaChi" runat="server" Width="371px" MaxLength="256" Text=""
                        CssClass='detailedViewTextBoxList ctrListForm' onfocus='TextBoxLeftFocus(this);'
                        onblur='TextBoxLeftBlur(this);'></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="6" class="dvtCellInfo">
                    <hr size="1" noshade="noshade">
                </td>
            </tr>
            <tr class="rowDetail" align="right">
                <td colspan="5" class="dvtCellListInfo" runat="server" style="text-align: right;
                    padding-right: 10px;">
                    Cộng thành tiền
                </td>
                <td>
                    <input type="text" runat="server" class="detailedViewTextBoxList_Disable1 ctrListForm txtNumber"
                        style="width: 80%;" disabled="disabled" id="txtTongThanhTien" maxlength="14"
                        value="0" />
                    <%=CurencyName %>
                </td>
            </tr>
            <tr class="rowDetail" align="right">
                <td colspan="6" class="dvtCellListInfo" id="tdForm" runat="server">
                </td>
            </tr>
            <tr class="rowDetail" align="right">
                <td colspan="6" class="dvtCellListInfo">
                    <table width="100%" cellspacing="0" cellpadding="0" border="0" class="listForm">
                        <tr>
                            <td align="right">
                                <input type="button" value="Thêm dòng nhập" onclick="AddRow();" id="btnAddRow">
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="height: 40px;" valign="bottom" colspan="6" align="center">
                    <asp:Button ID="btnDefault" runat="server" CssClass="buttonHidden" Text="" CausesValidation="false"
                        OnClientClick="return DefaultEnter();" />
                    <asp:Button ID="btnSave" runat="server" CssClass="crmbutton 
listForm edit" Text="Save" OnClick="btnSave_Click" CausesValidation="false" OnClientClick="return ValidatePhieuBanHang()" />
                    <asp:Button ID="btnXemBanIn" runat="server" CssClass="crmbutton
listForm edit" Text="Xem chi tiết" CausesValidation="false" Width="90px" OnClick="btnXemBanIn_Click" />
                    <asp:Button ID="btnReset" runat="server" CssClass="crmbutton 
listForm edit" Text="Save" OnClick="btnReset_Click" CausesValidation="false" />
                    <asp:Button ID="btnCacel" runat="server" CssClass="crmbutton 
listForm edit" Text="Cancel" OnClick="btnCacel_Click" CausesValidation="false" />
                </td>
            </tr>
        </table>
        <input type="hidden" id="hidTotalRow" value="" runat="server" />
        <input type="hidden" id="hidMaxRowShow" value="" runat="server" />
        <input type="hidden" id="hidCurrentRowShow" value="" runat="server" />
        <input type="hidden" id="hidOptionDVT" value="" runat="server" />

        <script language="javascript" type="text/javascript">
            var txtNgayLapPhieu = document.getElementById("<%= txtNgayLapPhieu.ClientID %>");
            SetupCalendar(txtNgayLapPhieu.id, "imgNgayLapPhieu");     
        </script>

        <asp:Literal ID="ltrScript" runat="server">
        </asp:Literal>

        <script type="text/javascript">
            var alredyFocus = false;

            function GetMHByname(value, index) {

                var maMH = Right(value, 6);
                //                rowMT = index;
                //                Admin_ChiTietPhieuNhapHang.GeMatHangInfor(maMH, GetMatHangInfor);
                //                /* document.getElementById('txtMaMH_' + index).value = maMH;*/
                CheckItem(maMH, index);
            }
            function DeleteRow(index) {
                var yes = confirm('Bạn muốn xóa dòng này?');
                if (yes) {
                    if (document.getElementById('tr_' + index)) {
                        document.getElementById('tr_' + index).style.display = 'none';

                        var hidCurrentRowShow = document.getElementById('<%=hidCurrentRowShow.ClientID %>').value;
                        hidCurrentRowShow = parseInt(hidCurrentRowShow, 0);
                        hidCurrentRowShow = hidCurrentRowShow - 1;
                        document.getElementById('<%=hidCurrentRowShow.ClientID %>').value = hidCurrentRowShow;
                        var oldValue = document.getElementById("txtThanhTien_" + index).value;
                        oldValue =ReplaceAllChar(removeComma(oldValue),",",".");
                        oldValue = parseFloat(oldValue);
                        if (isNaN(oldValue))
                            oldValue = 0;
                        var tongtien = document.getElementById('<%=txtTongThanhTien.ClientID %>').value;
                        tongtien = ReplaceAllChar(removeComma(tongtien),",",".");
                        tongtien = parseFloat(tongtien) - oldValue;
                        if (isNaN(tongtien))
                            tongtien = 0;
                        if (tongtien > 0)
                            document.getElementById('<%=txtTongThanhTien.ClientID %>').value = formatNumberAutoSeparate(ReplaceAllChar(tongtien.toString(),".",","));
                        else document.getElementById('<%=txtTongThanhTien.ClientID %>').value = '';

                    }
                }
            }

            function AddRow() {

                var current = document.getElementById('<%=hidMaxRowShow.ClientID %>').value;
                var maxRow = document.getElementById('<%=hidTotalRow.ClientID %>').value;
                maxRow = parseInt(maxRow, 0);
                current = parseInt(current, 0);
                current += 1;
                var hidCurrentRowShow = document.getElementById('<%=hidCurrentRowShow.ClientID %>').value;
                hidCurrentRowShow = parseInt(hidCurrentRowShow, 0);
                hidCurrentRowShow = hidCurrentRowShow + 1;
                if (hidCurrentRowShow > maxRow)
                    return;
                document.getElementById('tr_' + current).style.display = ''
                document.getElementById('<%=hidMaxRowShow.ClientID %>').value = current;

                document.getElementById('<%=hidCurrentRowShow.ClientID %>').value = hidCurrentRowShow;

            }
            window.scrollTo = function() {

            }
            function disableEnterKey(e, row, value) {
                var key;

                if (window.event)
                    key = window.event.keyCode;     //IE
                else
                    key = e.which;     //firefox

                //                alert(key);
                //                alert(row);
                //                alert(value);
            }
            var currentRowAccess = 0;
            function GetMatHang(e, index, value) {

                var key;

                if (window.event)
                    key = window.event.keyCode;     //IE
                else
                    key = e.which;     //firefox
                if (key == 13) {
                    if (value.length == 6) {
                        CheckItem(value, index);
                    }
                }

            }
            function GetMatHangByName(e, index, value) {

                var key;

                if (window.event)
                    key = window.event.keyCode;     //IE
                else
                    key = e.which;     //firefox
            //    document.getElementById('txtKeyPressKey').value = key;
                if (key == 13 || key == 0) {


                    GetMHByname(value, index);
                }

            }
            function GetMatHangByMaPhuTung(e, index, value) {

                var key;

                if (window.event)
                    key = window.event.keyCode;     //IE
                else
                    key = e.which;     //firefox

                if (key == 13 || key == 0) {
                    var kho = document.getElementById('<%=drlKho.ClientID %>').value;
                    currentRowAccess = index;
                    Admin_ChiTietPhieuBanHang.GeMatHangInforByMaPT(value, index, kho, GenerateMatHang);


                }

            }
            function GetMatHangInforByMaPT(response) {

                if (rowMT > 0) {
                    var arr = new Array();

                    arr = response.value.split('*+*');

                    if (arr.length > 1) {

                        SetControlvalue(document.getElementById('txtMaMH_' + rowMT), GetValueArray(arr, 0));
                        SetControlvalue(document.getElementById('txtMaPT_' + rowMT), GetValueArray(arr, 1));
                        SetControlvalue(document.getElementById('txtXe_' + rowMT), GetValueArray(arr, 2));
                        SetControlvalue(document.getElementById('txtNuocSX_' + rowMT), GetValueArray(arr, 3));
                        SetControlvalue(document.getElementById('txtHieu_' + rowMT), GetValueArray(arr, 4));
                        SetControlvalue(document.getElementById('drlDonViTinh_' + rowMT), GetValueArray(arr, 5));
                        SetControlvalue(document.getElementById('txtTenMH_' + rowMT), GetValueArray(arr, 6));
                        //  document.getElementById('txtGiaNhap_' + rowMT).value = '1000';
                        //objMatHang.MaMatHang + "*+*" + objMatHang.MaPhuTung + "*+*" + xe + "*+*" +   objMatHang.TenNuocSX + "*+*" + objMatHang.Hieu + "*+*" + objMatHang.DVT.ToString();

                    }

                }
                rowMT = 0;
            }
            function GetValueArray(arr, index) {
                try {
                    return arr[index].toString()
                }
                catch (e) {
                    return '';
                }
            }
            function SetControlvalue(ctr, value) {
                try {
                    if (ctr)
                        ctr.value = value;
                }
                catch (e) {
                }
            }
            function CheckItem(value, index) {
                //check data exist

                var maxrow = document.getElementById('<%=hidMaxRowShow.ClientID %>').value;
                maxrow = parseInt(maxrow);
                for (var i = 1; i <= maxrow; i++) {
                    if (document.getElementById('txtMaMH_' + i)) {
                        var maMH = document.getElementById('txtMaMH_' + i).value;
                        var tr = document.getElementById('tr_' + i);
                        if (tr) {
                            if (tr.style.display == '') {

                                if (maMH == value && index != i) {
                                    //alert('Mặt hàng này đã được nhập');
                                    document.getElementById('txtMaMH_' + index).value = '';
                                    document.getElementById('txtTenMH_' + index).value = '';
                                    document.getElementById('tr_' + i).style.display = '';
                                    // document.getElementById('txtMaMH_' + i).focus();
                                    return false;
                                }
                            }
                        }
                    }
                }
                currentRowAccess = index;
                var kho = document.getElementById('<%=drlKho.ClientID %>').value;
                Admin_ChiTietPhieuBanHang.GeMatHangInfor(value, index, kho, GenerateMatHang);
            }
            function GetThanhTien(row) {
                
                var dongia = document.getElementById('txtGiaNhap_' + row).value;

                dongia = ReplaceAllChar(removeComma(dongia), ",", ".");
                dongia = parseFloat(dongia);
                if (isNaN(dongia))
                    dongia = 0;
                var oldValue = document.getElementById("txtThanhTien_" + row).value;
                oldValue = ReplaceAllChar(removeComma(oldValue), ",", ".");
                oldValue = parseFloat(oldValue);
                if (isNaN(oldValue))
                    oldValue = 0;
                var soLuong = document.getElementById('txtSoLuong_' + row).value;
                soLuong = parseInt(soLuong);
                if (isNaN(soLuong))
                    soLuong = 0;
                dongia = soLuong * dongia;
                if (dongia > 0)

                    document.getElementById("txtThanhTien_" + row).value = formatNumberAutoSeparate(ReplaceAllChar(dongia.toString(), ".", ","));
                else document.getElementById("txtThanhTien_" + row).value = '';

                var tongtien = document.getElementById('<%=txtTongThanhTien.ClientID %>').value;
                tongtien = removeComma(tongtien);
                tongtien = parseFloat(ReplaceAllChar(tongtien, ",", "."));
                if (isNaN(tongtien))
                    tongtien = 0;
                tongtien = tongtien - oldValue;
                tongtien = tongtien + dongia;
                if (tongtien > 0)
                    document.getElementById('<%=txtTongThanhTien.ClientID %>').value = formatNumberAutoSeparate(ReplaceAllChar(tongtien.toString(),".", ","));
                else document.getElementById('<%=txtTongThanhTien.ClientID %>').value = '';
            }
            function GenerateMatHang(response) {


                var arr = new Array();
                arr = response.value.split('*|*');
                debugger;
                if (arr.length > 1) {
                    var oldGiaNhap = document.getElementById('txtGiaNhap_' + currentRowAccess).value;

                    oldGiaNhap = parseFloat(ReplaceAllChar(removeComma(oldGiaNhap), ",", "."));

                    if (isNaN(oldGiaNhap))
                        oldGiaNhap = 0;

                    document.getElementById('txtTenMH_' + currentRowAccess).value = arr[0].toString();
                    var maPT = arr[1].toString();
                   // maPT = '';
                    if (maPT != '') {
                        document.getElementById('txtMaPhuTung_' + currentRowAccess).value = maPT;
                        document.getElementById('txtMaPhuTung_' + currentRowAccess).className = 'detailedViewTextBoxList ctrListForm';
                        document.getElementById('txtMaPhuTung_' + currentRowAccess).disabled = false;
                    }
                    else {
                        document.getElementById('txtMaPhuTung_' + currentRowAccess).value = '';
                        document.getElementById('txtMaPhuTung_' + currentRowAccess).disabled = true;
                        document.getElementById('txtMaPhuTung_' + currentRowAccess).className = 'detailedViewTextBoxList_Disable1 ctrListForm';
                    }
                        document.getElementById('tdDVT_' + currentRowAccess).innerHTML = arr[2].toString();
                    //document.getElementById('drlDonViTinh_' + currentRowAccess).focus();
                    document.getElementById('drlDonViTinh_' + currentRowAccess).value = arr[2].toString();
                    //  document.getElementById('txtGiaNhap_' + currentRowAccess).value = arr[3].toString();
                    document.getElementById('txtSoLuongTon_' + currentRowAccess).value = arr[4].toString();
                    var dongia = document.getElementById('txtGiaNhap_' + currentRowAccess).value;
                    dongia = ReplaceAllChar(removeComma(dongia),",", ".");
                    dongia = parseFloat(dongia);
                    if (isNaN(dongia))
                        dongia = 0;
                    var soLuong = document.getElementById('txtSoLuong_' + currentRowAccess).value;
                    soLuong = parseInt(soLuong);
                    if (isNaN(soLuong))
                        soLuong = 0;
                    dongia = soLuong * dongia;
                    var oldThanhTien = document.getElementById("txtThanhTien_" + currentRowAccess).value;
                    oldThanhTien = parseFloat(ReplaceAllChar(removeComma(oldThanhTien), ",", "."));

                    if (isNaN(oldThanhTien))
                        oldThanhTien = 0;
                    document.getElementById("txtThanhTien_" + currentRowAccess).value = formatNumberAutoSeparate(ReplaceAllChar(dongia.toString(), ".", ","));
                    var tongtien = document.getElementById('<%=txtTongThanhTien.ClientID %>').value;
                    tongtien = removeComma(tongtien);
                    tongtien = parseFloat(ReplaceAllChar(tongtien, ",", "."));
                    tongtien = tongtien + dongia - oldThanhTien;
                    document.getElementById('<%=txtTongThanhTien.ClientID %>').value = formatNumberAutoSeparate(ReplaceAllChar(tongtien.toString(),".",","));
                    document.getElementById('txtMaMH_' + currentRowAccess).value = arr[5].toString();
                    document.getElementById('txtXe_' + currentRowAccess).value = arr[6].toString();
                }
                else {

                    document.getElementById('txtTenMH_' + currentRowAccess).value = '';
                    document.getElementById('txtMaPhuTung_' + currentRowAccess).value = '';
                    //document.getElementById('tdDVT_' + currentRowAccess).innerHTML = '';
                    document.getElementById('drlDonViTinh_' + currentRowAccess).focus();
                    //document.getElementById('drlDonViTinh_' + currentRowAccess).value = '';
                    //  document.getElementById('txtGiaNhap_' + currentRowAccess).value = arr[3].toString();
                    document.getElementById('txtSoLuongTon_' + currentRowAccess).value = '';
                    document.getElementById('txtSoLuong_' + currentRowAccess).value = '';
                    document.getElementById('txtMaMH_' + currentRowAccess).value = '';
                    document.getElementById('txtXe_' + currentRowAccess).value = '';
                }
                //if this is end row then add new Row
                var rowShow = document.getElementById('<%=hidCurrentRowShow.ClientID %>').value;
                rowShow = parseInt(rowShow);
                if (rowShow <= currentRowAccess)
                    AddRow();
                currentRowAccess = parseInt(currentRowAccess, 0) + 1;
                document.getElementById('txtMaMH_' + currentRowAccess).focus();
            }
            function CheckSoLuongTonMatHang(row, dvt) {
                currentRowAccess = row;
                var kho = document.getElementById('<%=drlKho.ClientID %>').value;
                var maMH = document.getElementById('txtMaMH_' + row).value;
                Admin_ChiTietPhieuBanHang.GetSoLuongTon(maMH, kho, dvt, GenerateSoLuongTon);

            }
            function GenerateSoLuongTon(response) {
                document.getElementById('txtSoLuongTon_' + currentRowAccess).value = response.value;
            }
            function CheckSoLuongBan(soluong, row) {
                var soLuongTon = document.getElementById('txtSoLuongTon_' + row).value;
                var soLuongTonTemp = document.getElementById('txtSoLuongTonTemp_' + row).value;
                soLuongTon = parseInt(soLuongTon);
                soLuongTonTemp = parseInt(soLuongTonTemp);
                soluong = parseInt(soluong);

                if (soluong <= 0) {
                    document.getElementById('txtSoLuong_' + row).focus();
                    alert('Số lượng bán phải lớn hơn 0');
                    return false;
                }
                /*if (soluong > (soLuongTon + soLuongTonTemp)) {
                    document.getElementById('txtSoLuong_' + row).focus();
                    alert('Số lượng bán phải nhở hơn số lượng tồn kho');
                    return false;
                }*/
            }
            function CheckSoLuongTonByRow(row) {
                var soluong = document.getElementById('txtSoLuong_' + row).value;
                var soLuongTon = document.getElementById('txtSoLuongTon_' + row).value;
                var soLuongTonTemp = document.getElementById('txtSoLuongTonTemp_' + row).value;
                soLuongTon = parseInt(soLuongTon);
                soLuongTonTemp = parseInt(soLuongTonTemp);
                soluong = parseInt(soluong);

                if (soluong <= 0) {
                    document.getElementById('txtSoLuong_' + row).focus();
                    return false;
                }
                if (soluong > (soLuongTon + soLuongTonTemp)) {
                    document.getElementById('txtSoLuong_' + row).focus();
                    return false;
                }
                return true;
            }
            function ValidatePhieuBanHang() {
                //check data here
                //check require kho

                var kho = document.getElementById('<%=drlKho.ClientID %>').value;
                if (kho == '') {
                    alert('Vui lòng chọn kho');
                    document.getElementById('<%=drlKho.ClientID %>').focus();
                    return false;
                }
                var NgayLapPhieu = document.getElementById('<%=txtNgayLapPhieu.ClientID %>').value;
                if (!isDate(NgayLapPhieu, "dd/MM/yyyy")) {
                    alert('Ngày lập phiếu không hợp lệ');
                    document.getElementById('<%=txtNgayLapPhieu.ClientID %>').focus();
                    return false;
                }
                var tenkh = document.getElementById('<%=txtTenKhachHang.ClientID %>').value;
                /*if (tenkh == '') {
                alert('Vui lòng nhập tên khách hàng');
                document.getElementById('<%=txtTenKhachHang.ClientID %>').focus();
                return false;
                }*/
                var diachi = document.getElementById('<%=txtDiaChi.ClientID %>').value;
                /*if (tenkh == '') {
                alert('Vui lòng nhập địa chỉ');
                document.getElementById('<%=txtDiaChi.ClientID %>').focus();
                return false;
                }*/
                //txtTenKhachHang,txtDiaChi
                //check valida date here
                //check Phieu Nhap Hang Data
                //delete end row
                var maxRow = document.getElementById('<%=hidMaxRowShow.ClientID %>').value;
                maxRow = parseInt(maxRow, 0);
                // var maMH = document.getElementById('txtMaMH_' + maxRow).value;
                //  if (maMH == '')
                //    document.getElementById('tr_' + maxRow).style.display = 'none';


                var data = '';
                var totalRow = document.getElementById('<%=hidMaxRowShow.ClientID %>').value;
                totalRow = parseInt(totalRow, 0);
                var inValidData = 1;
                for (var i = 1; i <= totalRow; i++) {
                    var tr = document.getElementById('tr_' + i);
                    if (tr) {
                        if (tr.style.display == '') {
                            var maMH = document.getElementById('txtMaMH_' + i).value;
                            if (maMH != '') {
                                if (!ValidataRow(i)) {
                                    alert('Vui lòng kiểm tra lại dữ liệu nhập tại dòng ' + inValidData);
                                    return false;
                                }
                                else {

                                    //MaMH|DVT|SL|GiaNhap
                                    var txtMaMH = document.getElementById('txtMaMH_' + i).value;
                                    var soluong = document.getElementById('txtSoLuong_' + i).value;
                                    var dvt = document.getElementById('drlDonViTinh_' + i).value;
                                    var giaNhap = document.getElementById('txtGiaNhap_' + i).value;
                                    data += txtMaMH + ";" + soluong + ";" + dvt + ";" + ReplaceAllChar(removeComma(giaNhap),",",".") + "*";
                                }
                            }
                            inValidData++;
                        }
                    }
                }
                if (data == '') {
                    document.getElementById('<%=btnSave.ClientID %>').focus();
                    alert('Vui lòng nhập thông tin từng mặt hàng cho phiếu bán hàng');
                    return false;
                }

                var nguoiNhap = '<%=userNameLogin %>';
                var command = '<%=Command %>';
                var MaPhieuBan = '<%=MaPhieuBan %>';
                Admin_ChiTietPhieuBanHang.InsertData(command, MaPhieuBan, kho, nguoiNhap, NgayLapPhieu, tenkh, diachi, data, InsertPhieuBanHangResult);
                return false;
            }
            function InsertPhieuBanHangResult(response) {
                if (response.value == '0') {
                    alert('Có lỗi xảy ra trong quá trình truy cập dữ liệi. Vui lòng thử lai.!');
                    return false;
                }
                else {
                    window.location.href = 'ViewPhieuBanHang.aspx?id=' + response.value;
                }
            }
            function ValidataRow(index) {

                //check require MatHang
                var matHang = document.getElementById('txtMaMH_' + index).value;
                if (matHang == '') {
                    // document.getElementById('txtMaMH_' + index).focus();
                    return true;
                }
                var dvt = document.getElementById('drlDonViTinh_' + index).value;
                if (dvt == '') {
                    document.getElementById('drlDonViTinh_' + index).focus();
                    return false;
                }
                //check require SoLuong
                var soluong = document.getElementById('txtSoLuong_' + index).value;
                if (soluong == '') {
                    document.getElementById('txtSoLuong_' + index).focus();
                    return false;
                }

                if (!CheckSoLuongTonByRow(index))
                    return false;

                //check gianhap is valid
                var giaban = document.getElementById('txtGiaNhap_' + index).value;
                if (giaban == '') {
                    document.getElementById('txtGiaNhap_' + index).focus();
                    return false;
                }

                if (!CheckCGiaIsValid(giaban)) {
                    document.getElementById('txtGiaNhap_' + index).focus();
                    return false;
                }
                return true;

            }

            var totalRow;
            function ChangeKho(id) {

                var lstMH = '';
                //Refresh so luong ton
                var isGetData = false;
                totalRow = document.getElementById('<%=hidMaxRowShow.ClientID %>').value;
                totalRow = parseInt(totalRow, 0);
                lstMH = '';
                var lstDVT = '';
                for (var i = 1; i <= totalRow; i++) {
                    var tr = document.getElementById('tr_' + i);
                    if (tr) {
                        if (tr.style.display == '') {
                            var mamh = document.getElementById('txtMaMH_' + i).value;
                            var dvt = document.getElementById('drlDonViTinh_' + i).value;
                            if (mamh != '')
                                isGetData = true;
                            lstMH += mamh + "#";
                            lstDVT += dvt + "#";
                        }
                    }
                }

                if (isGetData) {
                    var MaPhieuBan = '<%=MaPhieuBan %>';
                    Admin_ChiTietPhieuBanHang.GetListSoLuongTon(MaPhieuBan, lstMH, lstDVT, id, GenerateListSoLuongTon);

                }
                return false;

            }

            function GenerateListSoLuongTon(response) {

                var arr = new Array();
                arr = response.value.split('*');
                if (arr.length > 0) {
                    var arrSLTon = new Array();
                    var arrSLTonTemp = new Array();
                    arrSLTon = arr[0].toString().split('|');
                    arrSLTonTemp = arr[1].toString().split('|');
                    for (var i = 1; i <= totalRow; i++) {
                        document.getElementById('txtSoLuongTon_' + i).value = arrSLTon[i - 1].toString();
                        document.getElementById('txtSoLuongTonTemp_' + i).value = arrSLTonTemp[i - 1].toString();

                    }

                }
            }
            function DefaultEnter() {
                return false;
            }
        </script>

    </div>
    </form>
</body>
</html>
